<?php
namespace service;

use Flc\Dysms\Client;
use Flc\Dysms\Request\SendSms;
use service\SystemConfigService;
use think\Cache;

class AliMessageService
{
    public static $acsClient=null;

    public static function getAcsClient($expire=3600) {
        if(($accessKeyId=Cache::get('accessKeyId')) && ($accessKeySecret=Cache::get('accessKeySecret'))){
            $config=[
                'accessKeyId'    => $accessKeyId,
                'accessKeySecret' => $accessKeySecret
            ];
        }else{
            $config=[
                'accessKeyId'    => SystemConfigService::get('accessKeyId'),
                'accessKeySecret' => SystemConfigService::get('accessKeySecret')
            ];
            Cache::set('accessKeyId',$config['accessKeyId'],$expire);
            Cache::set('accessKeySecret',$config['accessKeySecret'],$expire);
        }
        if(!$config['accessKeyId'] || !$config['accessKeySecret']) exception('请配置accessKeyId和accessKeySecret');
        return $config;
    }

    /**
     * 发送短信
     * @param string $tel 短信接收号码
     * @param string $setSignName 短信签名
     * @param string $setTemplateCode 短信模板ID
     * @param array $setTemplateParam 短信内容
     * @param string $setOutId 外部流水扩展字段
     */
    public static function sendmsg($tel='',$setTemplateParam=[],$setOutId=''){
        $setSignName=Cache::get('smsSignName');
        $setTemplateCode=Cache::get('smsTemplateCode');
        if(!$setSignName || !$setTemplateCode){
            $setSignName=SystemConfigService::get('smsSignName');
            $setTemplateCode=SystemConfigService::get('smsTemplateCode');
            if($setSignName && $setTemplateCode){
                Cache::set('smsSignName',$setSignName);
                Cache::set('smsSignName',$setTemplateCode);
            }
        }
        if(!$setSignName || !$setTemplateCode) exception('请配置smsSignName和smsTemplateCode');
        $client  = new Client(self::getAcsClient());
        $sendSms = new SendSms;
        $sendSms->setPhoneNumbers($tel);
        $sendSms->setSignName($setSignName);
        $sendSms->setTemplateCode($setTemplateCode);
        $sendSms->setTemplateParam(is_array($setTemplateParam)?$setTemplateParam:['code'=>$setTemplateParam]);
        $sendSms->setOutId($setOutId);
        return $client->execute($sendSms);
    }

    /**
     * 发送家长上传作业短信通知
     * @Author   FW
     * @DateTime 2021-02-08
     * @Email    123763712@qq.com
     * @param    string           $tel              [description]
     * @param    array            $setTemplateParam [description]
     * @param    string           $setOutId         [description]
     * @return   [type]                             [description]
     */
    public static function sendUploadHomeworkNotice($tel='',$parent_name='',$setOutId=''){
        $client  = new Client(['accessKeyId' => 'LTAI4FriYqmfenoKfe5tuiM6','accessKeySecret' => 'bsEZFuOifSvXVzCh314Eo2Ru6bLHDY']);
        $sendSms = new SendSms;
        $sendSms->setPhoneNumbers($tel);
        $sendSms->setSignName('逗你学');
        $sendSms->setTemplateCode('SMS_211481735');
        $sendSms->setTemplateParam(['parent_name'=> $parent_name]);
        $sendSms->setOutId($setOutId);

        return $client->execute($sendSms);
    }


    /**
     * 生成随机验证码
     * @return int
     */
    public static function getVerificationCode($length=6)
    {
        $str='123456789';
        $code='';
        for($i=0;$i<$length;$i++){
            $code.= $str[mt_rand(0,strlen($str)-1)];
        }
        return $code;
    }
    /**
     * 生成随机填充码
     * @return int
     */
    public static function getFillCode($length=4)
    {
        $str='zxcvbnmasdfghjklqwertyuiop';
        $code='';
        for($i=0;$i<$length;$i++){
            $code.= $str[mt_rand(0,strlen($str)-1)];
        }
        return $code;
    }

}